package com.zone.vehicle.superApps.datasource;

import android.content.Context;

import com.zone.lib.download.DownloadHelper;
import com.zone.lib.download.DownloadStatus;
import com.zone.lib.download.db.DownloadDaoUtilsStore;
import com.zone.lib.download.db.entity.Download;
import com.zone.lib.download.install.AndroidP;
import com.zone.sdk.common.http.base.BaseRequest;
import com.zone.sdk.common.http.base.PageResult;
import com.zone.sdk.common.http.base.Result;
import com.zone.sdk.common.http.callback.ResultCallBack;
import com.zone.sdk.module.appstore.base.AppStoreResult;
import com.zone.sdk.module.appstore.entity.AppEntity;
import com.zone.sdk.module.appstore.request.AppsDetailRequest;
import com.zone.sdk.module.comment.base.CommentPage;
import com.zone.sdk.module.comment.entity.AppCommentScoreEntity;
import com.zone.sdk.module.comment.entity.CommentListEntity;
import com.zone.sdk.module.comment.entity.RecommendEntity;
import com.zone.sdk.module.comment.entity.RecommendListEntity;
import com.zone.sdk.module.comment.entity.SearchResultEntity;
import com.zone.sdk.module.comment.request.AppSearchRequest;
import com.zone.sdk.module.comment.request.CommentListRequest;
import com.zone.sdk.module.comment.request.CommentScoreRequest;
import com.zone.sdk.module.comment.request.RecommendListRequest;
import com.zone.sdk_api.ApiManager;
import com.zone.vehicle.sdk.module.superapp.request.SyncStatusRequest;
import com.zone.vehicle.superApps.utils.SharedPreferencesUtil;
import com.zone.vehicle.superApps.utils.TimeUtil;

import org.apache.commons.lang3.ObjectUtils;

import java.io.File;
import java.util.Date;
import java.util.List;

import okhttp3.ResponseBody;

public class HomeDataSource {

    public void indexSearch(String search, ResultCallBack<Result<CommentPage<SearchResultEntity>>> callback){
        AppSearchRequest request = new AppSearchRequest();
        request.setPageNum(0);
        request.setPageSize(10);
        request.setSearchName(search);
        request.setPageTime(TimeUtil.getNowTime());
        request.setSearchType(0);
        ApiManager.getInstance().indexSearch(request, callback);
    }

    public void getHomeAppListData(ResultCallBack<Result<AppStoreResult<AppEntity>>> callBack){
        ApiManager.getInstance().homeAppList(new BaseRequest(), callBack);
    }

    public void getAppDetail(String code, ResultCallBack<Result<AppEntity>> callBack){
        AppsDetailRequest request = new AppsDetailRequest();
        request.setCode(code);
        ApiManager.getInstance().appsDetail(request, callBack);
    }

    public void appList(List<String> codeList, ResultCallBack<Result<List<AppEntity>>> callBack){
        AppsDetailRequest request = new AppsDetailRequest();
        request.setCodeList(codeList);
        ApiManager.getInstance().appList(request, callBack);
    }

    public void commentList(String code, ResultCallBack<Result<CommentListEntity>> callback){
        CommentListRequest request = new CommentListRequest();
        request.setObjectId(code);
        request.setPageNum(0);
        request.setPageSize(10);
        ApiManager.getInstance().commentCommentList(request, callback);
    }

    public void commentInitAmwayWallList(ResultCallBack<Result<RecommendListEntity>> callback){
        RecommendListRequest request = new RecommendListRequest();
        request.setPageNum(0);
        request.setPageSize(10);
        request.setPageTime(TimeUtil.getNowTime());
        ApiManager.getInstance().commentInitAmwayWallList(request, callback);
    }

    public void getRecommendListApp(int pageNo, int pageSize, ResultCallBack<Result<PageResult<RecommendEntity>>> callBack){
        RecommendListRequest baseRequest = new RecommendListRequest();
        ApiManager.getInstance().getRecommendListApp(baseRequest, callBack);
    }

    public void commentScore(String objectId, ResultCallBack<Result<AppCommentScoreEntity>> callback){
        CommentScoreRequest request = new CommentScoreRequest();
        request.setObjectId(objectId);
        ApiManager.getInstance().commentScore(request, callback);
    }

    public void downLoadApp(Context context, AppEntity appEntity, ResultCallBack<ResponseBody> callBack){

//        if (ObjectUtils.isNotEmpty(appEntity)){
//            File dir = new File(context.getCacheDir(), "download");
//            if (!dir.exists()){
//                dir.mkdirs();
//            }
//            File saveFile = new File(dir, appEntity.getCode()+".apk");
//            AppsDownloadRequest request = new AppsDownloadRequest();
//            request.setAppCode(appEntity.getCode());
//            request.setMode("0");
//            request.setRequestId(""+System.currentTimeMillis());
//            ApiManager.getInstance().appsDownload(saveFile, request, callBack);
//
//            UploadAppStatus.sendAppState(appEntity.getType(), appEntity.getCode(), DownloadStatus.PREPARE_DOWNLOAD, "0", "0");
//        }
    }

    public void syncStatus(SyncStatusRequest request, ResultCallBack<Result> callback){
        ApiManager.getInstance().syncStatus(request, callback);
    }

    public void downLoadApp(Context context, int status, AppEntity appEntity){
        if (ObjectUtils.isNotEmpty(appEntity)){
//            File dir = new File(context.getExternalCacheDir(), "download");
            File dir = new File(context.getCacheDir(), "download");
            if (!dir.exists()){
                dir.mkdirs();
            }
            File saveFile = new File(dir, appEntity.getCode()+".apk");
            Download download = DownloadDaoUtilsStore.getInstance().getDownloadByCode(appEntity.getCode());
            if (ObjectUtils.isNotEmpty(download)){
                DownloadDaoUtilsStore.getInstance().deleteByCode(download.getCode());
                download = null;
            }
            if (ObjectUtils.isEmpty(download)){
                download = new Download();
                download.setCode(appEntity.getCode());
                download.setDownloadStatus(status);
                download.setDownloadUrl(appEntity.getUrl());
                download.setFilePath(saveFile.getPath());
                download.setAppType("0");
                download.setType("0");
                download.setDownloadLocation(0);
                download.setType(""+appEntity.getType());
                download.setVersion(appEntity.getVersion());
                download.setCreateTime(new Date().toString());
                DownloadDaoUtilsStore.getInstance().getDownloadAppDaoUtils().insert(download);
//                DownloadHelper.getInstance().addTask(appEntity.getCode(), saveFile, status, Long.parseLong(ObjectUtils.isEmpty(appEntity.getSize())?"0":appEntity.getSize()), appEntity.getVersion(), SharedPreferencesUtil.getInstance(context).getSP("vin")).submit(context);
                DownloadHelper.getInstance().addTask(saveFile, SharedPreferencesUtil.getInstance(context).getSP("vin"), status, appEntity, download).submit(context);
            } else {
                //暂停 或者 继续
                if (status == DownloadStatus.PAUSE_DOWNLOAD || status == DownloadStatus.PREPARE_DOWNLOAD
                        || status == DownloadStatus.DELETE_DOWNLOAD
//                        (download.getDownloadStatus() == DownloadStatus.PREPARE_DOWNLOAD
//                    || download.getDownloadStatus() == DownloadStatus.LOADING_DOWNLOAD)
                ){
//                    DownloadHelper.getInstance().addTask(appEntity.getCode(), saveFile, status, Long.parseLong(appEntity.getSize()), appEntity.getVersion(), SharedPreferencesUtil.getInstance(context).getSP("vin")).submit(context);
                    DownloadHelper.getInstance().addTask(saveFile, SharedPreferencesUtil.getInstance(context).getSP("vin"), status, appEntity, download).submit(context);
                }
            }
        }
    }

    public void installApp(Context context, String appCode){
        Download download = DownloadDaoUtilsStore.getInstance().getDownloadByCode(appCode);
//        if (ObjectUtils.isNotEmpty(download) && download.getDownloadStatus() == DownloadStatus.COMPLETE){
//        AndroidP.install28(context, context.getCacheDir()+"/download/demo.apk", context.getPackageManager());
        AndroidP.install(context, context.getCacheDir()+"/download/demo.apk", context.getPackageManager(), "com.zone.speechtest");
//        }
    }
}